/**
 * 
 */

package com.the9tcat.funnyalarmclock;

import com.the9tcat.funnyalarmclock.oauth.Oauth;
import com.the9tcat.funnyalarmclock.sina.SinaListener;
import com.the9tcat.funnyalarmclock.sina.SinaWeibo;
import com.the9tcat.funnyalarmclock.util.Logger;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

/**
 * @author jianhpin
 */
public class AccountActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.account);
        final Button sinaBtn = (Button) findViewById(R.id.btn_sina);
        sinaBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SinaWeibo sw = new SinaWeibo(AccountActivity.this,
                                             new Listener());
                sw.doOauth();
                sinaBtn.setVisibility(View.GONE);
            }
        });
    }

    class Listener implements SinaListener {

        /*
         * (non-Javadoc)
         * @see
         * com.the9tcat.funnyalarmclock.sina.SinaListener#onAuthorizeError()
         */
        @Override
        public void onAuthorizeError() {

        }

        /*
         * (non-Javadoc)
         * @see
         * com.the9tcat.funnyalarmclock.sina.SinaListener#onAuthorized(android
         * .os.Bundle)
         */
        @Override
        public void onAuthorized(Bundle data) {
            Logger.d("***", "hello!!!");
            
            String oauth_token = data.getString("oauth_token");
            String oauth_token_secret = data.getString("oauth_token_secret");
//            String oauth_verifier = data.getString("oauth_verifier");
            String user_id = data.getString("user_id");

            ImageView iv = new ImageView(AccountActivity.this);
            TextView tv = new TextView(AccountActivity.this);

            LinearLayout ll = (LinearLayout) findViewById(R.id.account_layout);
            ll.addView(iv);
            ll.addView(tv);

            String info = Oauth.getAuthorizedContent(
                                                     "http://api.t.sina.com.cn/users/show/"
                                                             + user_id
                                                             + ".json",
                                                     "3509039754",
                                                     "888ff6f1acdce23fe024c28900128387",
                                                     oauth_token,
                                                     oauth_token_secret, null);
            try {
                JSONObject jo = new JSONObject(info);
                try {
                    HttpURLConnection hc = (HttpURLConnection) new URL(jo
                            .getString("profile_image_url")).openConnection();
                    InputStream is = hc.getInputStream();
                    Bitmap b = BitmapFactory.decodeStream(is);
                    iv.setImageBitmap(b);
                } catch (MalformedURLException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                tv.setText("名字：" + jo.getString("screen_name") + "\n位置："
                        + jo.getString("location") + "\n最新微博："
                        + jo.getJSONObject("status").getString("text"));
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }
}
